package com.kikoulol.jarmoryextractor;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import com.kikoulol.jarmoryextractor.utils.Utils;

public class ArenaTeam {

	private String armoryUrl;
	private Element arenaTeamXml;
	private List<Character> members = new ArrayList<Character>();

	private String name;
	private String url;
	private String teamUrlEscape;
	private String teamUrl;
	private String teamSize;
	private String size;
	private String seasonGamesWon;
	private String seasonGamesPlayed;
	private String season;
	private String relevance;
	private String realmUrl;
	private String realm;
	private String rating;
	private String ranking;
	private String lastSeasonRanking;
	private String gamesWon;
	private String gamesPlayed;
	private String faction;
	private String factionId;
	private String created;
	private String battleGroup;


	public ArenaTeam(String url) throws IOException, ParserConfigurationException, SAXException {
		armoryUrl = "http://" + url.split("/")[2] + "/";
		String xml = Utils.getArmoryXml(url);
		try {
			arenaTeamXml = Utils.getXmlRootElement(xml);
		}
		catch (Exception e) {
			File fileToRemove = new File(Utils.getLocalUrl(url));
			fileToRemove.delete();
			xml = Utils.getArmoryXml(url);
			arenaTeamXml = Utils.getXmlRootElement(xml);
		}
		init(arenaTeamXml);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public List<Character> getMembers() throws IOException, ParserConfigurationException, SAXException {
		for (int i = 0; i < arenaTeamXml.getElementsByTagName("character").getLength(); i++) {
			Node member = arenaTeamXml.getElementsByTagName("character").item(i);
			Character myChar = new Character(armoryUrl + "character-sheet.xml?" + member.getAttributes().getNamedItem("charUrl").getNodeValue());
			addMembre(myChar);
		}
		return members;
	}

	private void init(Element arenaTeamXml) {
		Node arenaTeam = arenaTeamXml.getElementsByTagName("arenaTeam").item(0);
		setName(arenaTeam.getAttributes().getNamedItem("name").getNodeValue());
		setBattleGroup(arenaTeam.getAttributes().getNamedItem("battleGroup").getNodeValue());
		setCreated(arenaTeam.getAttributes().getNamedItem("created").getNodeValue());
		setFaction(arenaTeam.getAttributes().getNamedItem("faction").getNodeValue());
		setFactionId(arenaTeam.getAttributes().getNamedItem("factionId").getNodeValue());
		setGamesPlayed(arenaTeam.getAttributes().getNamedItem("gamesPlayed").getNodeValue());
		setGamesWon(arenaTeam.getAttributes().getNamedItem("gamesWon").getNodeValue());
		setLastSeasonRanking(arenaTeam.getAttributes().getNamedItem("lastSeasonRanking").getNodeValue());
		setRanking(arenaTeam.getAttributes().getNamedItem("ranking").getNodeValue());
		setRating(arenaTeam.getAttributes().getNamedItem("rating").getNodeValue());
		setRealm(arenaTeam.getAttributes().getNamedItem("realm").getNodeValue());
		setRealmUrl(arenaTeam.getAttributes().getNamedItem("realmUrl").getNodeValue());
		setRelevance(arenaTeam.getAttributes().getNamedItem("relevance").getNodeValue());
		setSeason(arenaTeam.getAttributes().getNamedItem("season").getNodeValue());
		setSeasonGamesPlayed(arenaTeam.getAttributes().getNamedItem("seasonGamesPlayed").getNodeValue());
		setSeasonGamesWon(arenaTeam.getAttributes().getNamedItem("seasonGamesWon").getNodeValue());
		setSize(arenaTeam.getAttributes().getNamedItem("size").getNodeValue());
		setTeamSize(arenaTeam.getAttributes().getNamedItem("teamSize").getNodeValue());
		setTeamUrl(arenaTeam.getAttributes().getNamedItem("teamUrl").getNodeValue());
		setTeamUrlEscape(arenaTeam.getAttributes().getNamedItem("name").getNodeValue());
		setUrl(arenaTeam.getAttributes().getNamedItem("url").getNodeValue());
	}


	public void addMembre(Character character) {
		members.add(character);
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getTeamUrlEscape() {
		return teamUrlEscape;
	}

	public void setTeamUrlEscape(String teamUrlEscape) {
		this.teamUrlEscape = teamUrlEscape;
	}

	public String getTeamUrl() {
		return teamUrl;
	}

	public void setTeamUrl(String teamUrl) {
		this.teamUrl = teamUrl;
	}

	public String getTeamSize() {
		return teamSize;
	}

	public void setTeamSize(String teamSize) {
		this.teamSize = teamSize;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getSeasonGamesWon() {
		return seasonGamesWon;
	}

	public void setSeasonGamesWon(String seasonGamesWon) {
		this.seasonGamesWon = seasonGamesWon;
	}

	public String getSeasonGamesPlayed() {
		return seasonGamesPlayed;
	}

	public void setSeasonGamesPlayed(String seasonGamesPlayed) {
		this.seasonGamesPlayed = seasonGamesPlayed;
	}

	public String getSeason() {
		return season;
	}

	public void setSeason(String season) {
		this.season = season;
	}

	public String getRelevance() {
		return relevance;
	}

	public void setRelevance(String relevance) {
		this.relevance = relevance;
	}

	public String getRealmUrl() {
		return realmUrl;
	}

	public void setRealmUrl(String realmUrl) {
		this.realmUrl = realmUrl;
	}

	public String getRealm() {
		return realm;
	}

	public void setRealm(String realm) {
		this.realm = realm;
	}

	public String getRating() {
		return rating;
	}

	public void setRating(String rating) {
		this.rating = rating;
	}

	public String getRanking() {
		return ranking;
	}

	public void setRanking(String ranking) {
		this.ranking = ranking;
	}

	public String getLastSeasonRanking() {
		return lastSeasonRanking;
	}

	public void setLastSeasonRanking(String lastSeasonRanking) {
		this.lastSeasonRanking = lastSeasonRanking;
	}

	public String getGamesWon() {
		return gamesWon;
	}

	public void setGamesWon(String gamesWon) {
		this.gamesWon = gamesWon;
	}

	public String getGamesPlayed() {
		return gamesPlayed;
	}

	public void setGamesPlayed(String gamesPlayed) {
		this.gamesPlayed = gamesPlayed;
	}

	public String getFaction() {
		return faction;
	}

	public void setFaction(String faction) {
		this.faction = faction;
	}

	public String getCreated() {
		return created;
	}

	public void setCreated(String created) {
		this.created = created;
	}

	public String getBattleGroup() {
		return battleGroup;
	}

	public void setBattleGroup(String battleGroup) {
		this.battleGroup = battleGroup;
	}

	public String getFactionId() {
		return factionId;
	}

	public void setFactionId(String factionId) {
		this.factionId = factionId;
	}

}
